package com.lcm.learn.lcmcode.ThreadLocal;

import java.util.concurrent.*;

/**
 * @description:
 * @author: lcm
 * @create: 2020-01-16 10:38
 **/

public class Test {

    private static ThreadLocal<Integer> local= ThreadLocal.withInitial(()->new Integer(1));

    public static void main(String[] args){
        test1();
        test2();
    }

    private static void test1(){
        Executor executor=new ThreadPoolExecutor(1, 1,
                1L, TimeUnit.MINUTES,
                new LinkedBlockingDeque<>(100));

        for(int i=0;i<5;i++){
            executor.execute(() -> {
                Integer j = local.get();
                j=j<<1;
                local.set(j);
                System.out.println(Thread.currentThread().getName()+":"+j);
            });
        }
    }

    private static void test2(){
        Executor executor=new ThreadPoolExecutor(1, 1,
                1L, TimeUnit.MINUTES,
                new LinkedBlockingDeque<>(100));

        for(int i=0;i<5;i++){
            executor.execute(() -> {
                Integer j = local.get();
                j=j<<1;
                local.set(j);
                System.out.println(Thread.currentThread().getName()+":"+j);
                local.remove();
            });
        }
    }
}
